package cn.com.xiaoshoujia.quartz;

import cn.com.xiaoshoujia.activities.vo.ActivitiyBackMsgVO;
import cn.com.xiaoshoujia.application.service.WalletService;
import cn.com.xiaoshoujia.company.entity.ComAccountLog;
import cn.com.xiaoshoujia.company.mapper.ComAccountLogMapper;
import cn.com.xiaoshoujia.constants.*;
import cn.com.xiaoshoujia.domain.dto.WalletLogsDTO;
import cn.com.xiaoshoujia.infrastructure.utils.Result;
import cn.com.xiaoshoujia.order.dto.RefundDTO;
import cn.com.xiaoshoujia.order.mapper.ActivityOrderMapper;
import cn.com.xiaoshoujia.order.mapper.RefundMapper;
import cn.com.xiaoshoujia.user.entity.UserAccountLog;
import cn.com.xiaoshoujia.user.mapper.UserAccountLogMapper;
import cn.com.xiaoshoujia.wxpay.config.WeixinPayConfig;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


public class TeamRefundTask implements Job {

    private Logger logger = LoggerFactory.getLogger(TeamRefundTask.class);

    @Autowired
    private RefundMapper refundMapper;

    @Autowired
    private ActivityOrderMapper activityOrderMapper;

    @Autowired
    private ComAccountLogMapper comAccountLogMapper;

    @Autowired
    private UserAccountLogMapper userAccountLogMapper;

    @Autowired
    WalletService walletService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        logger.info("team wx 退款 start");
        List<RefundDTO> refundDTOList = refundMapper.findAll();
        if (refundDTOList != null && refundDTOList.size() > 0) {
            for (RefundDTO refundDTO : refundDTOList) {
                try {
                    logger.info("team wx 退款 orderId = {}", refundDTO.getOrderId());
                    int truePrice = refundDTO.getPrice().multiply(BigDecimal.valueOf(100)).intValue();
                    logger.info("team wx 退款 price = {}", truePrice);
                    WeixinPayConfig.refund(refundDTO.getOrderId(), String.valueOf(truePrice));
                } catch (Exception e) {
                    logger.error("team wx TeamRefundTask error {}", e.toString());
                }
                ActivitiyBackMsgVO activitiyBackMsg = activityOrderMapper.notifyMsg(refundDTO.getOrderId());
                ComAccountLog comAccountLog = new ComAccountLog(null, activitiyBackMsg.getComId(), (byte) 2,
                        refundDTO.getOrderId(), refundDTO.getPrice().multiply(BigDecimal.valueOf(-1)),
                        activitiyBackMsg.getProductName(), activitiyBackMsg.getFkBsProductId(), (byte) 1, new Date(),
                        (byte) 1, 1, activitiyBackMsg.getUserName(), "拼团活动退款", (byte) 30);
                comAccountLogMapper.save(comAccountLog);
                UserAccountLog userAccountLog = new UserAccountLog(null, activitiyBackMsg.getSysId(), (byte) 1, (byte) 1,
                        activitiyBackMsg.getId(), refundDTO.getPrice(), activitiyBackMsg.getProductName(),
                        activitiyBackMsg.getFkBsProductId(), new Date(), (byte) 1, (byte) 30);
                userAccountLogMapper.save(userAccountLog);

                /*钱包服务 start*/
                String fkCompanyXcode = activitiyBackMsg.getFkCompanyXcode();
                try {
                    Result result1 = walletService.findByXcode(fkCompanyXcode);
                    //企业未绑定钱包
                    if ("false".equals(result1.getCode())) {
                        //绑定钱包
                        Result result2 = walletService.save(fkCompanyXcode);
                        if ("FAIL".equals(result2.getCode())) {
                            logger.error("拼团未成功退款 {}", result2.getObject().toString());
                            throw new RuntimeException(result2.getObject().toString());
                        }
                    }
                } catch (Exception e) {
                    logger.error(e.toString());
                }
                WalletLogsDTO walletLogsDTO = new WalletLogsDTO();
                walletLogsDTO.setOpeartor(fkCompanyXcode);
                walletLogsDTO.setOpeartorType(0);
                walletLogsDTO.setType(1);
                walletLogsDTO.setFkCompanyXcode(fkCompanyXcode);
                walletLogsDTO.setPrice(refundDTO.getPrice());
                walletLogsDTO.setOrderId(refundDTO.getOrderId());
                walletLogsDTO.setBillingType(BillingTypeEnum.EXPENDITURE.getCode());
                walletLogsDTO.setBusinessType(BusinessTypeEnum.REFUND.getCode());
                walletLogsDTO.setFundsFrom(FundsFromEnum.FUNDS_POOL.getCode());
                walletLogsDTO.setFundsTo(FundsToEnum.PERSONAL_WECHAT.getCode());
                walletLogsDTO.setScene(Scene.TEAM_HEAD_FAIL.getCode());
                try {
                    Result result3 = walletService.saveWalletLogs(walletLogsDTO);
                    if ("FAIL".equals(result3.getCode())) {
                        logger.error("拼团未成功退款 {}", result3.getObject().toString());
                        throw new RuntimeException(result3.getObject().toString());
                    }
                } catch (Exception e) {
                    logger.error(e.toString());
                }
                /*钱包服务 end*/

            }
            refundMapper.updateList(refundDTOList);
        }
    }
}
